package leetcode;

import java.lang.reflect.Array;
import java.util.*;

public class LeetCodeMain60 {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        if (k<=0||input.length<=0){
            return null;
        }
        if (input.length<k){
            return null;
        }
        PriorityQueue<Integer> queue=new PriorityQueue<>(((o1, o2) -> o2-o1));
        for (int i = 0; i < input.length; i++) {
            if (queue.isEmpty()||queue.size()<k){
                queue.add(input[i]);
            }else if (!queue.isEmpty()){
                int tmp=queue.poll();
                if (tmp>input[i]){
                    queue.add(input[i]);
                }else {
                    queue.add(tmp);
                }
            }
        }
        ArrayList<Integer> list=new ArrayList<>();
        for (int a:queue) {
            list.add(a);
        }
        return list;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Integer> list=new ArrayList<>();
        int num=in.nextInt();
        int k=in.nextInt();
        int[] input=new int[num];
        for (int i = 0; i < num; i++) {
            input[i]=in.nextInt();
        }
        PriorityQueue<Integer> queue=new PriorityQueue<>(((o1, o2) -> o2-o1));
        for (int i = 0; i < input.length; i++) {
            if (queue.isEmpty()||queue.size()<k){
                queue.add(input[i]);
            }else if (!queue.isEmpty()){
                int tmp=queue.poll();
                if (tmp>input[i]){
                    queue.add(input[i]);
                }else {
                    queue.add(tmp);
                }
            }
        }
        for (int a:queue) {
            list.add(a);
        }
        for (int integer:list) {
            System.out.println(integer);
        }
    }
    public static void main5(String[] args) {
        Scanner in=new Scanner(System.in);
        String string=in.next();
        String str="";
        StringBuffer buffer=new StringBuffer();
        for (int i = 0; i < string.length(); i++) {
            char ch=string.charAt(i);
            if (ch>='0'&&ch<='9'){
                buffer.append(ch);
            }else if (!Character.isDigit(ch)){
                if (buffer.length()>str.length()){
                    str=buffer.toString();
                }
                buffer.delete(0,buffer.length());
            }
        }
        if (buffer.length()>0&&str.length()<buffer.length()){
            str=buffer.toString();
        }
        System.out.println(str);
    }
    public static boolean isAdmin(String userId){
        return userId . toLowerCase( )==" admin";
    }
    public static void main4(String[] args){
        System. out. println(isAdmin( "Admin"));
    }

    public static void main3(String[] args) {
        StringBuffer a=new StringBuffer("A");
        StringBuffer b=new StringBuffer("B");
        operate(a,b);
        System.out.println(a.toString());
        System.out.println(b.toString());
    }

    private static void operate(StringBuffer a, StringBuffer b) {
        a.append(b);
        b=a;
    }

    public int maxSubArray(int[] nums) {
        int length=nums.length;
        if (length==0){
            return 0;
        }
        int[] dp=new int[length];
        int max=nums[0];
        dp[0]=nums[0];
        for (int i = 1; i < length; i++) {
            dp[i]=Math.max(dp[i-1]+nums[i],nums[i]);
            if (max<dp[i]){
                max=dp[i];
            }
        }
        return max;
    }
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int length1=nums1.length;
        int length2=nums2.length;
        int[][] dp=new int[length1+1][length2+1];
        for (int i = 1; i <= length1; i++) {
            for (int j = 1; j <= length2; j++) {
                if (nums1[i-1]==nums2[j-1]){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else {
                    dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[length1][length2];
    }
    public int longestCommonSubsequence(String text1, String text2) {
        int length1=text1.length();
        int length2=text2.length();
        int[][] dp=new int[length1+1][length2+1];
        for (int i = 1; i <= length1; i++) {
            for (int j = 1; j <= length2; j++) {
                if (text1.charAt(i-1)==text2.charAt(j-1)){
                    dp[i][j]=dp[i-1][j-1]+1;
                }else {
                    dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        return dp[length1][length2];
    }
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int num=in.nextInt();
            int abl=in.nextInt();
            int[] arr=new int[num];
            for (int i = 0; i < num; i++) {
                arr[i]=in.nextInt();
            }
            for (int i = 0; i < num; i++) {
                if (arr[i]<=abl){
                    abl+=arr[i];
                }else {
                    abl+=isNum(arr[i],abl);
                }
            }
            System.out.println(abl);
        }
    }
    public static int isNum(int num1,int num2){
        int max=0;
        for (int i = 1; i < num2*num1; i++) {
            if (num1%i==0&&num2%i==0){
                max=i;
            }
        }
        return max;
    }
    public static void main1(String[] args) {
        Scanner in=new Scanner(System.in);
        String  str=in.nextLine();
        int[] arr=new int[256];
        int flag=0;
        for (int i = 0; i < str.length(); i++) {
            char ch=str.charAt(i);
            arr[ch-'0']++;
        }
        for (int i = 0; i < str.length(); i++) {
            char ch=str.charAt(i);
            if (arr[ch-'0']==1){
                flag=1;
                System.out.println(ch);
                break;
            }
        }
        if (flag==0){
            System.out.println(-1);
        }
    }
}
